<template>
  <el-dialog :visible="isShowDialog" title="添加房产" @close="btnCancel">
    <el-form label-width="88px">
      <el-form-item label="经纪">
        <el-select v-model="formData.agent" placeholder="经纪">
          <el-option v-for="item in agentList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="分类">
        <el-select v-model="formData.category" placeholder="分类">
          <el-option v-for="item in cateList" :key="item.id" :value="item.id" :label="item.title" />
        </el-select>
      </el-form-item>
      <el-form-item label="标题">
        <el-input v-model="formData.title" />
      </el-form-item>
      <el-form-item label="标签">
        <el-tag
          v-for="tag in formData.tags"
          :key="tag"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)"
        >
          {{ tag }}
        </el-tag>
        <el-input
          v-if="inputVisible"
          ref="saveTagInput"
          v-model="inputValue"
          class="input-new-tag"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        />
        <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 标签</el-button>
      </el-form-item>
      <el-form-item label="地址">
        <el-row type="flex">
          <el-cascader
            v-model="map"
            :options="options"
            :props="{value: 'label'}"
          />
          <el-input v-model="formData.address" />
        </el-row>
      </el-form-item>
      <el-form-item label="价格">
        <el-input v-model="formData.price" />
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="formData.desc" />
      </el-form-item>
      <el-form-item label="内容">
        <VueEditor v-model="formData.content" />
      </el-form-item>
      <el-form-item label="介绍图片">
        <ImageUpload v-if="isShowDialog" ref="photos" :limit="3" />
      </el-form-item>
      <el-form-item label="封面">
        <ImageUpload v-if="isShowDialog" ref="image" :limit="1" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="btnOK">确定</el-button>
      <el-button @click="btnCancel">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { VueEditor } from 'vue2-editor'
import ImageUpload from '@/components/ImageUpload'
import mapData from '@/utils/map.json'
import { addProperty, editProperty } from '@/api/property'
console.log('地图数据', mapData)
export default {
  components: { VueEditor, ImageUpload },
  props: {
    isShowDialog: {
      type: Boolean,
      default: false
    },
    agentList: {
      type: Array,
      required: true
    },
    cateList: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      map: [],
      options: mapData,
      formData: {
        'title': '',
        'desc': '',
        'image': '',
        'agent': '',
        'content': '',
        'address': '',
        'price': '',
        'photos': [], // 图片, url 组成数组
        'tags': [], // 标签, 字符串组成数组
        'category': '' // 属于哪个分类 id 值
      },
      // 标签相关
      inputVisible: false,
      inputValue: ''
    }
  },
  methods: {
    async btnOK() {
      const data = {
        ...this.formData,
        address: this.map.join(' ') + '-' + this.formData.address,
        image: this.$refs.image.fileList[0].url,
        photos: this.$refs.photos.fileList.map(item => item.url)
      }
      console.log(data)
      // 发请求
      if (data.id) {
        await editProperty(data)
      } else {
        await addProperty(data)
      }
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.$emit('closeDialog')
      // 更新页面
      this.$emit('reloadList')
    },
    btnCancel() {
      // 清理数据
      // 关闭弹窗
      this.formData = {
        'title': '',
        'desc': '',
        'image': '',
        'agent': '',
        'content': '',
        'address': '',
        'price': '',
        'photos': [], // 图片, url 组成数组
        'tags': [], // 标签, 字符串组成数组
        'category': '' // 属于哪个分类 id 值
      }
      this.$emit('closeDialog')
    },
    handleClose(tag) {
      this.formData.tags.splice(this.formData.tags.indexOf(tag), 1)
    },

    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },

    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue) {
        this.formData.tags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    }
  }
}
</script>

<style>
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
